//
//  HDidentityAuthenticationVC.swift
//  HuaDaoPeifu
//
//  Created by ZERO on 2018/6/15.
//  Copyright © 2018年 paiyipai. All rights reserved.
//

import UIKit

class HDidentityAuthenticationVC: UIViewController, SinglePickerViewDelegate {
    var accountType:HDUserType?
    fileprivate var progressAlert = HDProgressAlert()
    
    var regStr:PYPReg?
    fileprivate var typeID:String?
    fileprivate var typeInfo:String?
    
    fileprivate var selectedAgentCode : String?

    @IBOutlet weak var typeLabel: UILabel!
    @IBOutlet weak var agentLabel: UILabel!
    @IBOutlet weak var IDView: UIView!
    @IBOutlet weak var nameView: UIView!
    @IBOutlet weak var numberView: UIView!
    @IBOutlet weak var agentView: UIView!
    @IBOutlet weak var selectBtn: UIButton!
    @IBOutlet weak var commitBtn: UIButton!
    
    /// 姓名
    @IBOutlet weak var nameTF: UITextField!
    /// 证件号TF
    @IBOutlet weak var cretcodeTF: UITextField!
    
    fileprivate var typePicker: SinglePickerView!
    fileprivate var agentPicker: SinglePickerView!

    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "身份认证"
        setUI()
        // Do any additional setup after loading the view.
    }

    
    func setUI() {
        IDView.layer.borderWidth = 1
        IDView.layer.cornerRadius = 3
        IDView.layer.borderColor = UIColor(hex: 0xdbdbdb).cgColor
        
        nameView.layer.borderWidth = 1
        nameView.layer.cornerRadius = 3
        nameView.layer.borderColor = UIColor(hex: 0xdbdbdb).cgColor
        
        numberView.layer.borderWidth = 1
        numberView.layer.cornerRadius = 3
        numberView.layer.borderColor = UIColor(hex: 0xdbdbdb).cgColor
        
        agentView.layer.borderWidth = 1
        agentView.layer.cornerRadius = 3
        agentView.layer.borderColor = UIColor(hex: 0xdbdbdb).cgColor
        
        selectBtn.layer.cornerRadius = 3
        selectBtn.layer.borderWidth = 1
        selectBtn.layer.borderColor = UIColor(red:(255/255.0), green:(138/255.0), blue:(0/255.0), alpha:1.0).cgColor
        
        commitBtn.layer.cornerRadius = 3

        typePicker                 = SinglePickerView()
        typePicker?.type = SingleListType(rawValue: 1)!
        typePicker?.delegate            = self
        typeID = typePicker.defaultID
        typeInfo = typePicker.defaultVal
        
        agentPicker                 = SinglePickerView()
        agentPicker?.type = SingleListType.insuranceCompany;
        agentPicker?.delegate       = self
    }
    
    
// 证件类型
    @IBAction func typeSelect(_ sender: UIButton) {
        typePicker?.showInView()
    }
    
// 机构选择
    @IBAction func agentSelect(_ sender: UIButton) {
        agentPicker?.showInView()
    }
    
    func complete(_ pickerView: UIView!, tempStr str: String!, idStr idstr: String!) {
         if pickerView == self.typePicker  {
            typeLabel.text = str
            typeID = idstr
            typeInfo = str
        }else if pickerView == agentPicker  {
            agentLabel.text = str
            selectedAgentCode = idstr;
        }
        
    }
    
    @IBAction func reselectAction(_ sender: Any) {
        let select = HDIdentitySelectVC()
        select.userType = accountType
        select.ToWhere = 2
        navigationController?.pushViewController(select, animated: true)
        
    }
    
    
    @IBAction func commitAction(_ sender: UIButton) {
        // 证件号
        if cretcodeTF.text == nil || cretcodeTF.text == "" {
            progressAlert.enterCretCode()
            return
        }
        // 姓名
        if nameTF.text == nil || nameTF.text == "" {
            progressAlert.enterCretCode()
            return
        }
        // 投保机构
        if selectedAgentCode == nil {
            progressAlert.selectInsurance()
            return
        }
        
        self.regStr?.crittype = typeID
        self.regStr?.crittype_info = typeInfo
        self.regStr?.critcode = cretcodeTF.text
        self.regStr?.name = nameTF.text
        self.regStr?.insurance = selectedAgentCode
        
        guard let regParam = self.regStr else {
            return
        }
        
        HDApiManagerHandler.requestReg_new(regParam) {[unowned self] (result:HDApiResult?) in
            guard let result = result else{
                // 网络请求失败
                return
            }
            // 请求成功
            if (result.status == HDApiResultStatus.Success){
                // 页面跳转，-- 页面状态更新
                guard let data = result.data,
                let accountInfo = HDAccountInfo.yy_model(withJSON: data) else {
                    return
                }
                accountInfo.name = regParam.name
                accountInfo.phone = regParam.name
                HDAccountHandler.saveUserInfo(accountInfo: accountInfo)
                self.navigationController?.popToRootViewController(animated: true)
            }else{
                HDIdentityTipsView.initHDIdentityTipsView(showMessage: result.msg!)

            }
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
